/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.base.Splitter;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.Disposition;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.AttributeFlag;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Identity;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.Signature;
/*     */ import com.zimbra.cs.account.Zimlet;
/*     */ import com.zimbra.cs.account.accesscontrol.Right;
/*     */ import com.zimbra.cs.account.accesscontrol.RightManager;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.UserServlet;
/*     */ import com.zimbra.cs.service.admin.AdminAccessControl;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.session.SoapSession;
/*     */ import com.zimbra.cs.util.BuildInfo;
/*     */ import com.zimbra.cs.zimlet.ZimletPresence;
/*     */ import com.zimbra.cs.zimlet.ZimletUserProperties;
/*     */ import com.zimbra.cs.zimlet.ZimletUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.account.type.Prop;
/*     */ import java.util.ArrayList;
/*     */ import java.util.EnumSet;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetInfo
/*     */   extends AccountDocumentHandler
/*     */ {
/*  76 */   private static ArrayList<GetInfoExt> extensions = new ArrayList();
/*     */   
/*     */   public static void addExtension(GetInfoExt extension) {
/*  79 */     synchronized (extensions) {
/*  80 */       extensions.add(extension);
/*     */     }
/*     */   }
/*     */   
/*     */   private static enum Section {
/*  85 */     MBOX,  PREFS,  ATTRS,  ZIMLETS,  PROPS,  IDENTS,  SIGS,  DSRCS,  CHILDREN;
/*     */     
/*     */     private Section() {}
/*     */     
/*  89 */     static Section lookup(String value) throws ServiceException { try { return valueOf(value.toUpperCase().trim());
/*     */       } catch (IllegalArgumentException iae) {
/*  91 */         throw ServiceException.INVALID_REQUEST("unknown GetInfo section: " + value.trim(), null);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  98 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  99 */     Account account = getRequestedAccount(zsc);
/*     */     
/* 101 */     if (!canAccessAccount(zsc, account)) {
/* 102 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*     */     
/*     */ 
/* 106 */     String secstr = request.getAttribute("sections", null);
/*     */     Set<Section> sections;
/* 108 */     Set<Section> sections; if (secstr != null) {
/* 109 */       sections = EnumSet.noneOf(Section.class);
/* 110 */       for (String sec : Splitter.on(',').omitEmptyStrings().trimResults().split(secstr)) {
/* 111 */         sections.add(Section.lookup(sec));
/*     */       }
/*     */     } else {
/* 114 */       sections = EnumSet.allOf(Section.class);
/*     */     }
/*     */     
/* 117 */     String rightsStr = request.getAttribute("rights", null);
/* 118 */     Set<Right> rights = null;
/* 119 */     RightManager rightMgr; if (rightsStr != null) {
/* 120 */       rightMgr = RightManager.getInstance();
/* 121 */       rights = Sets.newHashSet();
/* 122 */       for (String right : Splitter.on(',').omitEmptyStrings().trimResults().split(rightsStr)) {
/* 123 */         rights.add(rightMgr.getUserRight(right));
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 128 */     Element response = zsc.createElement(AccountConstants.GET_INFO_RESPONSE);
/* 129 */     response.addAttribute("version", BuildInfo.FULL_VERSION, Element.Disposition.CONTENT);
/* 130 */     response.addAttribute("id", account.getId(), Element.Disposition.CONTENT);
/* 131 */     response.addAttribute("name", account.getUnicodeName(), Element.Disposition.CONTENT);
/*     */     try {
/* 133 */       response.addAttribute("crumb", zsc.getAuthToken().getCrumb(), Element.Disposition.CONTENT);
/*     */     }
/*     */     catch (AuthTokenException e) {
/* 136 */       ZimbraLog.account.warn("can't generate crumb", e);
/*     */     }
/* 138 */     long lifetime = zsc.getAuthToken().getExpires() - System.currentTimeMillis();
/* 139 */     response.addAttribute("lifetime", lifetime, Element.Disposition.CONTENT);
/*     */     
/* 141 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 144 */     AuthToken authToken = zsc.getAuthToken();
/* 145 */     if (authToken.isDelegatedAuth()) {
/* 146 */       Account admin = prov.get(Key.AccountBy.id, authToken.getAdminAccountId());
/* 147 */       if (admin != null) {
/* 148 */         boolean isAdmin = AdminAccessControl.isAdequateAdminAccount(admin);
/* 149 */         if (isAdmin) {
/* 150 */           response.addAttribute("adminDelegated", true, Element.Disposition.CONTENT);
/*     */         }
/*     */       }
/*     */     }
/*     */     try
/*     */     {
/* 156 */       Server server = prov.getLocalServer();
/* 157 */       if (server != null) {
/* 158 */         response.addAttribute("docSizeLimit", server.getFileUploadMaxSize());
/*     */       }
/* 160 */       Config config = prov.getConfig();
/* 161 */       if (config != null) {
/* 162 */         long maxAttachSize = config.getMtaMaxMessageSize();
/* 163 */         if (maxAttachSize == 0L) {
/* 164 */           maxAttachSize = -1L;
/*     */         }
/* 166 */         response.addAttribute("attSizeLimit", maxAttachSize);
/*     */       }
/*     */     }
/*     */     catch (ServiceException e) {}
/*     */     
/* 171 */     if ((sections.contains(Section.MBOX)) && (Provisioning.onLocalServer(account))) {
/* 172 */       response.addAttribute("rest", UserServlet.getRestUrl(account), Element.Disposition.CONTENT);
/*     */       try {
/* 174 */         Mailbox mbox = getRequestedMailbox(zsc);
/* 175 */         response.addAttribute("used", mbox.getSize(), Element.Disposition.CONTENT);
/*     */         
/* 177 */         Session s = (Session)context.get("zimbra.session");
/* 178 */         if ((s instanceof SoapSession))
/*     */         {
/* 180 */           response.addAttribute("prevSession", ((SoapSession)s).getPreviousSessionTime(), Element.Disposition.CONTENT);
/*     */           
/* 182 */           response.addAttribute("accessed", ((SoapSession)s).getLastWriteAccessTime(), Element.Disposition.CONTENT);
/*     */           
/* 184 */           response.addAttribute("recent", ((SoapSession)s).getRecentMessageCount(), Element.Disposition.CONTENT);
/*     */         }
/*     */         else
/*     */         {
/* 188 */           long lastAccess = mbox.getLastSoapAccessTime();
/* 189 */           response.addAttribute("prevSession", lastAccess, Element.Disposition.CONTENT);
/* 190 */           response.addAttribute("accessed", lastAccess, Element.Disposition.CONTENT);
/* 191 */           response.addAttribute("recent", mbox.getRecentMessageCount(), Element.Disposition.CONTENT);
/*     */         }
/*     */       }
/*     */       catch (ServiceException e) {}
/*     */     }
/*     */     
/*     */ 
/* 198 */     doCos(account, response);
/*     */     
/* 200 */     Map<String, Object> attrMap = account.getUnicodeAttrs();
/* 201 */     Locale locale = Provisioning.getInstance().getLocale(account);
/*     */     
/* 203 */     if (sections.contains(Section.PREFS)) {
/* 204 */       Element prefs = response.addUniqueElement("prefs");
/* 205 */       GetPrefs.doPrefs(account, prefs, attrMap, null);
/*     */     }
/* 207 */     if (sections.contains(Section.ATTRS)) {
/* 208 */       Element attrs = response.addUniqueElement("attrs");
/* 209 */       doAttrs(account, locale.toString(), attrs, attrMap);
/*     */     }
/* 211 */     if (sections.contains(Section.ZIMLETS)) {
/* 212 */       Element zimlets = response.addUniqueElement("zimlets");
/* 213 */       doZimlets(zimlets, account);
/*     */     }
/* 215 */     if (sections.contains(Section.PROPS)) {
/* 216 */       Element props = response.addUniqueElement("props");
/* 217 */       doProperties(props, account);
/*     */     }
/* 219 */     if (sections.contains(Section.IDENTS)) {
/* 220 */       Element ids = response.addUniqueElement("identities");
/* 221 */       doIdentities(ids, account);
/*     */     }
/* 223 */     if (sections.contains(Section.SIGS)) {
/* 224 */       Element sigs = response.addUniqueElement("signatures");
/* 225 */       doSignatures(sigs, account);
/*     */     }
/* 227 */     if (sections.contains(Section.DSRCS)) {
/* 228 */       Element ds = response.addUniqueElement("dataSources");
/* 229 */       doDataSources(ds, account);
/*     */     }
/* 231 */     if (sections.contains(Section.CHILDREN)) {
/* 232 */       Element ca = response.addUniqueElement("childAccounts");
/* 233 */       doChildAccounts(ca, account, zsc.getAuthToken());
/*     */     }
/*     */     
/* 236 */     if ((rights != null) && (!rights.isEmpty())) {
/* 237 */       Element eRights = response.addUniqueElement("rights");
/* 238 */       doDiscoverRights(eRights, account, rights);
/*     */     }
/*     */     
/* 241 */     GetAccountInfo.addUrls(response, account);
/*     */     
/* 243 */     for (GetInfoExt extension : extensions) {
/* 244 */       extension.handle(zsc, response);
/*     */     }
/* 246 */     return response;
/*     */   }
/*     */   
/*     */   static void doCos(Account acct, Element response) throws ServiceException {
/* 250 */     Cos cos = Provisioning.getInstance().getCOS(acct);
/* 251 */     if (cos != null) {
/* 252 */       Element eCos = response.addUniqueElement("cos");
/* 253 */       eCos.addAttribute("id", cos.getId());
/* 254 */       eCos.addAttribute("name", cos.getName());
/*     */     }
/*     */   }
/*     */   
/*     */   static void doAttrs(Account acct, String locale, Element response, Map<String, Object> attrsMap) throws ServiceException
/*     */   {
/* 260 */     AttributeManager attrMgr = AttributeManager.getInstance();
/*     */     
/* 262 */     Set<String> attrList = attrMgr.getAttrsWithFlag(AttributeFlag.accountInfo);
/*     */     
/* 264 */     Set<String> acctAttrs = attrMgr.getAllAttrsInClass(AttributeClass.account);
/* 265 */     Set<String> domainAttrs = attrMgr.getAllAttrsInClass(AttributeClass.domain);
/* 266 */     Set<String> serverAttrs = attrMgr.getAllAttrsInClass(AttributeClass.server);
/* 267 */     Set<String> configAttrs = attrMgr.getAllAttrsInClass(AttributeClass.globalConfig);
/*     */     
/* 269 */     Provisioning prov = Provisioning.getInstance();
/* 270 */     Domain domain = prov.getDomain(acct);
/* 271 */     Server server = acct.getServer();
/* 272 */     Config config = prov.getConfig();
/*     */     
/* 274 */     for (String key : attrList) {
/* 275 */       Object value = null;
/* 276 */       if ("zimbraLocale".equals(key)) {
/* 277 */         value = locale;
/* 278 */       } else if ("zimbraAttachmentsBlocked".equals(key))
/*     */       {
/* 280 */         value = (config.isAttachmentsBlocked()) || (acct.isAttachmentsBlocked()) ? "TRUE" : "FALSE";
/*     */       }
/*     */       else {
/* 283 */         value = attrsMap.get(key);
/*     */         
/* 285 */         if ((value == null) && 
/* 286 */           (!acctAttrs.contains(key)))
/*     */         {
/* 288 */           if (domainAttrs.contains(key)) {
/* 289 */             if (domain != null) {
/* 290 */               value = domain.getMultiAttr(key);
/*     */             }
/* 292 */           } else if (serverAttrs.contains(key)) {
/* 293 */             value = server.getMultiAttr(key);
/* 294 */           } else if (configAttrs.contains(key)) {
/* 295 */             value = config.getMultiAttr(key);
/*     */           }
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 301 */       ToXML.encodeAttr(response, key, value);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void doZimlets(Element response, Account acct)
/*     */   {
/*     */     try {
/* 308 */       ZimletUtil.migrateUserPrefIfNecessary(acct);
/*     */       
/* 310 */       ZimletPresence userZimlets = ZimletUtil.getUserZimlets(acct);
/* 311 */       List<Zimlet> zimletList = ZimletUtil.orderZimletsByPriority(userZimlets.getZimletNamesAsArray());
/* 312 */       int priority = 0;
/* 313 */       for (Zimlet z : zimletList) {
/* 314 */         if ((z.isEnabled()) && (!z.isExtension())) {
/* 315 */           ZimletUtil.listZimlet(response, z, priority, userZimlets.getPresence(z.getName()));
/*     */         }
/* 317 */         priority++;
/*     */       }
/*     */       
/*     */ 
/* 321 */       ZimletUtil.listDevZimlets(response);
/*     */     } catch (ServiceException se) {
/* 323 */       ZimbraLog.account.error("can't get zimlets", se);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void doProperties(Element response, Account acct) {
/* 328 */     ZimletUserProperties zp = ZimletUserProperties.getProperties(acct);
/* 329 */     Set<? extends Prop> props = zp.getAllProperties();
/* 330 */     for (Prop prop : props) {
/* 331 */       Element elem = response.addElement("prop");
/* 332 */       elem.addAttribute("zimlet", prop.getZimlet());
/* 333 */       elem.addAttribute("name", prop.getName());
/* 334 */       elem.setText(prop.getValue());
/*     */     }
/*     */   }
/*     */   
/*     */   private static void doIdentities(Element response, Account acct) {
/*     */     try {
/* 340 */       for (Identity i : Provisioning.getInstance().getAllIdentities(acct)) {
/* 341 */         ToXML.encodeIdentity(response, i);
/*     */       }
/*     */     } catch (ServiceException e) {
/* 344 */       ZimbraLog.account.error("can't get identities", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void doSignatures(Element response, Account acct) {
/*     */     try {
/* 350 */       List<Signature> signatures = Provisioning.getInstance().getAllSignatures(acct);
/* 351 */       for (Signature s : signatures) {
/* 352 */         ToXML.encodeSignature(response, s);
/*     */       }
/*     */     } catch (ServiceException e) {
/* 355 */       ZimbraLog.account.error("can't get signatures", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void doDataSources(Element response, Account acct) {
/*     */     try {
/* 361 */       List<DataSource> dataSources = Provisioning.getInstance().getAllDataSources(acct);
/* 362 */       for (DataSource ds : dataSources) {
/* 363 */         if (!ds.isInternal()) {
/* 364 */           com.zimbra.cs.service.mail.ToXML.encodeDataSource(response, ds);
/*     */         }
/*     */       }
/*     */     } catch (ServiceException e) {
/* 368 */       ZimbraLog.mailbox.error("Unable to get data sources", e);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void doChildAccounts(Element response, Account acct, AuthToken authToken) throws ServiceException {
/* 373 */     String[] childAccounts = acct.getMultiAttr("zimbraChildAccount");
/* 374 */     String[] visibleChildAccounts = acct.getMultiAttr("zimbraPrefChildVisibleAccount");
/*     */     
/* 376 */     if ((childAccounts.length == 0) && (visibleChildAccounts.length == 0)) {
/* 377 */       return;
/*     */     }
/* 379 */     Provisioning prov = Provisioning.getInstance();
/* 380 */     Set<String> children = new HashSet(childAccounts.length);
/*     */     
/* 382 */     for (String childId : visibleChildAccounts) {
/* 383 */       if (!children.contains(childId))
/*     */       {
/*     */ 
/* 386 */         Account child = prov.get(Key.AccountBy.id, childId, authToken);
/* 387 */         if (child != null) {
/* 388 */           encodeChildAccount(response, child, true);
/*     */         }
/* 390 */         children.add(childId);
/*     */       }
/*     */     }
/* 393 */     for (String childId : childAccounts)
/* 394 */       if (!children.contains(childId))
/*     */       {
/*     */ 
/* 397 */         Account child = prov.get(Key.AccountBy.id, childId, authToken);
/* 398 */         if (child != null) {
/* 399 */           encodeChildAccount(response, child, false);
/*     */         }
/* 401 */         children.add(childId);
/*     */       }
/*     */   }
/*     */   
/*     */   protected Element encodeChildAccount(Element parent, Account child, boolean isVisible) {
/* 406 */     Element elem = parent.addElement("childAccount");
/* 407 */     elem.addAttribute("id", child.getId());
/* 408 */     elem.addAttribute("name", child.getUnicodeName());
/* 409 */     elem.addAttribute("visible", isVisible);
/* 410 */     elem.addAttribute("active", child.isAccountStatusActive());
/*     */     
/* 412 */     String displayName = child.getAttr("displayName");
/* 413 */     if (displayName != null) {
/* 414 */       Element attrsElem = elem.addUniqueElement("attrs");
/* 415 */       attrsElem.addKeyValuePair("displayName", displayName, "attr", "name");
/*     */     }
/*     */     
/* 418 */     return elem;
/*     */   }
/*     */   
/*     */   private void doDiscoverRights(Element eRights, Account account, Set<Right> rights) throws ServiceException {
/* 422 */     DiscoverRights.discoverRights(account, rights, eRights, false);
/*     */   }
/*     */   
/*     */   public static abstract interface GetInfoExt
/*     */   {
/*     */     public abstract void handle(ZimbraSoapContext paramZimbraSoapContext, Element paramElement);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/GetInfo.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */